<?php
/* vim: set expandtab tabstop=4 shiftwidth=4: */
// +----------------------------------------------------------------------+
// | PHP version 5                                                        |
// +----------------------------------------------------------------------+
// | Copyright (c) 1997-2004 The PHP Group                                |
// +----------------------------------------------------------------------+
// | This source file is subject to version 3.0 of the PHP license,       |
// | that is bundled with this package in the file LICENSE, and is        |
// | available through the world-wide-web at the following url:           |
// | http://www.php.net/license/3_0.txt.                                  |
// | If you did not receive a copy of the PHP license and are unable to   |
// | obtain it through the world-wide-web, please send a note to          |
// | license@php.net so we can mail you a copy immediately.               |
// +----------------------------------------------------------------------+
// | Authors: Original Author <author@example.com>                        |
// |          Your Name <you@example.com>                                 |
// +----------------------------------------------------------------------+
//
// $Id:$
namespace Api;
use Dao\UserDao;
use Usb\UserUsb;
/**
 *@api Api::result('1','自定义内容',$data); 这种写法适用直接访问方法
 *@api $data = UserDao::selUser() trait单例 防止dao层new多次
 *@api Redis::set('name', 'ts-frame'); var_dump( Redis::get('name') ); //redis调用示例
 *@api Async::get('user/consuming');//异步耗时方法调用 控制层+方法名
 */
class User extends Api implements UserUsb {
    public function selUser($GET) {
        $name = $GET['name'] ?? '';
        $ucid = $GET['ucid'] ?? '';
        $flag = $GET['flag'] ?? '';
        $pageSize = $GET["pageSize"] ?? 10;
        $pageNum = $pageSize * (($GET["pageNum"] ?? 1) - 1);//小伙子看不懂吧
        $arr = UserDao::selUser($name, $ucid, $flag, $pageNum, $pageSize);
        return [1, '自定义内容', $arr[0], $arr[1], $pageSize];
    }
    public function editUser($POST) {
        $data = $POST['editForm'];
        if (UserDao::editUser($data) > 0) {
            return [1, '自定义内容'];
        } else return [0, '自定义内容'];
    }
    public function addUser($POST) {
        $data = $POST['addForm'];
        if (UserDao::addUser($data) > 0) {
            return [1, '自定义内容'];
        } else return [0, '自定义内容'];
    }
    public function delUser($POST) {
        $ids = $POST['Form']['ids'];
        if (UserDao::delUser($ids) > 0) {
            return [1, "自定义内容"];
        } else return [0, '自定义内容'];
    }
    /**
     * @Copyright: ©TS-Frame-Tiny-Speed
     * @description: 调用数据库原生方法
     * @param  GET http://localhost/TS/user/just
     * @return JSON
     * @author: TS
     * @Date: 2021-06-11 07:52:16
     */
    public function just() {
        $sql = "insert into tb_user (account,password,name,flag,ucid) values";
        for ($i = 0; $i < 3; $i++) {
            $sql.= "('ts$i','yyds','yyds','1','1'),";//批量录入永远滴神yyds
        };
        $sql = substr($sql, 0, strlen($sql) - 1);
        try {
            DB::query($sql);//原生PDO->query方法 也可以调用其他参考utils/mapper
            Api::result('1', '自定义内容');
        }
        catch(\Exception $e) {
            Api::result('0', $e);
        }
    }
    //模拟耗时
    public function consuming() {
        $i = 0;
        for ($i; $i < 300000000; $i++) {}
        echo 222;
    }
}
